package com.rrjjg.generate;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.IDbQuery;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.querys.ClickHouseQuery;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.OracleQuery;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collections;
import java.util.function.Supplier;

/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  mybatis-plus生成工具类 <br>具体参考
 * https://baomidou.com/pages/981406/#%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE-globalconfig
 * https://baomidou.com/pages/981406/#entity-%E7%AD%96%E7%95%A5%E9%85%8D%E7%BD%AE
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2022/7/4 11:12 <br>
 */
@Slf4j
public class MyBatisGenerator {

    private DBConfig dbConfig;

    public MyBatisGenerator(DBConfig dbConfig) {
        this.dbConfig = dbConfig;
    }

//    public static void main(String[] args) {
//        DBConfig dbConfig = new DBConfig();
//        dbConfig.setDbType("mysql")
//                .setDbUrl("jdbc:mysql://192.168.56.160:3306/config")
//                .setUserName("root")
//                .setTbPrefix("")
//                .setTables("PROJECT")
//                .setPassword("123456");
//        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(dbConfig);
//        myBatisGenerator.generator();
//    }
    public void generator() {
        String rootPath = System.getProperty("user.dir");
        Supplier<IDbQuery> supplier = ()->{
            switch (dbConfig.getDbType()){
                case "mysql":
                    return new MySqlQuery();
                case "clickhouse":
                    return new ClickHouseQuery();
                case "oracle":
                default:
                    return new OracleQuery();
            }
        };
        FastAutoGenerator.create(dbConfig.getDbUrl(), dbConfig.getUserName(), dbConfig.getPassword())
                .globalConfig(builder -> {
                    builder.author(dbConfig.getAuthor()) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(rootPath + dbConfig.getJavaPath()); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(dbConfig.getPack()) // 设置父包名
                            .moduleName(dbConfig.getModuleName()) // 设// 置父包模块名
                            .service("service")
                            .mapper("dao.mapper")
                            .xml("mapper.xml")
                            .entity("dao.entity")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, rootPath + dbConfig.getMapperPath())); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(Arrays.asList(dbConfig.getTables().split(","))) // 设置需要生成的表名
                            .addTablePrefix(Arrays.asList(dbConfig.getTbPrefix().split(",")).stream().toArray(String[]::new))
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .entityBuilder()
                            .enableChainModel()
                            .enableTableFieldAnnotation()
                            .enableLombok().build(); // 设置过滤表前缀
                })
//                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


}
